{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.__all__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback\n",
    "from xtquant.xttype import StockAccount\n",
    "import random\n",
    "\n",
    "class MyXtQuantTraderCallback(XtQuantTraderCallback):\n",
    "    def on_disconnected(self):\n",
    "        \"\"\"\n",
    "        连接状态回调\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        print(\"connection lost\")\n",
    "    def on_stock_order(self, order):\n",
    "        \"\"\"\n",
    "        委托信息推送\n",
    "        :param order: XtOrder对象\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        pass\n",
    "    def on_stock_trade(self, trade):\n",
    "        \"\"\"\n",
    "        成交信息推送\n",
    "        :param trade: XtTrade对象\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        pass\n",
    "\n",
    "def create_trader(account_id, mini_qmt_path):\n",
    "    # 创建session_id\n",
    "    session_id = int(random.randint(100000, 999999))\n",
    "    # 创建交易对象\n",
    "    xt_trader = XtQuantTrader(mini_qmt_path, session_id)\n",
    "    # 启动交易对象\n",
    "    xt_trader.start()\n",
    "    # 连接客户端\n",
    "    connect_result = xt_trader.connect()\n",
    "\n",
    "    if connect_result == 0:\n",
    "        print(f\"【miniQMT连接成功】 路径:{mini_qmt_path}\")\n",
    "\n",
    "    # 创建账号对象\n",
    "    account = StockAccount(account_id)\n",
    "    # 订阅账号\n",
    "    xt_trader.subscribe(account)\n",
    "    print(f\"【账号订阅成功】 账号ID:{account_id}\")\n",
    "    # 注册回调类\n",
    "    xt_trader.register_callback(MyXtQuantTraderCallback())\n",
    "\n",
    "    return xt_trader, account\n",
    "\n",
    "account_id = 'xxxx' # 你的账号ID\n",
    "mini_qmt_path = r'xxxx' # 你的miniQMT路径\n",
    "\n",
    "xt_trader, account = create_trader(account_id, mini_qmt_path)\n",
    "\n",
    "res = xt_trader.query_stock_asset(account)\n",
    "\n",
    "res.total_asset\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
